<!--
 * @Description: 弹出层，添加部门组件
 * @Author: yue
 * @Date: 2022-09-01 18:19:03
 * @LastEditors: yue
 * @LastEditTime: 2022-09-03 16:34:19
-->

<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="close">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 80%" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width: 80%" placeholder="请选择" @focus="getEmployees">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width: 80%" placeholder="1-300个字符" type="textarea" :rows="3"></el-input>
      </el-form-item>
    </el-form>

    <!-- 弹出层底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="close">取消</el-button>
        <el-button type="primary" size="small" @click="addDept">确认</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, editDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true
    },
    obj: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkSameName = async (rule, value, callback) => {
      const { depts } = await getDepartments()
      let flag = false
      if (this.formData.id) {
        flag = depts.filter((item) => item.pid === this.obj.id && item.id !== this.formData.id).some((item) => item.name === value)
      } else {
        flag = depts.filter((item) => item.pid === this.obj.id).some((item) => item.name === value)
      }
      flag ? callback(new Error(`${value} 已存在`)) : callback()
    }
    const checkSameCode = async (rule, value, callback) => {
      const { depts } = await getDepartments()
      let flag = false
      if (this.formData.id) {
        flag = depts.some((item) => item.code === value && item.id !== this.formData.id)
      } else {
        flag = depts.some((item) => item.code === value && value)
      }
      flag ? callback(new Error(`${value} 已存在`)) : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, trigger: 'blur', message: '部门名称不能为空' },
          { min: 2, max: 50, trigger: 'blur', message: '部门名称要求2-50个字符' },
          { validator: checkSameName, trigger: 'blur' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '部门编码不能为空' },
          { min: 2, max: 50, trigger: 'blur', message: '部门名称要求2-50个字符' },
          { validator: checkSameCode, trigger: 'blur' }
        ],
        manager: [{ required: true, trigger: 'blur', message: '部门负责人不能为空' }],
        introduce: [
          { required: true, trigger: 'blur', message: '部门介绍不能为空' },
          { min: 2, max: 300, trigger: 'blur', message: '部门名称要求2-300个字符' }
        ]
      },
      employees: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployees() {
      this.employees = await getEmployeeSimple()
    },
    addDept() {
      this.$refs.formData.validate(async (isOk) => {
        if (isOk) {
          if (this.formData.id) {
            await editDepartments(this.formData)
          } else {
            await addDepartments({ ...this.formData, pid: this.obj.id })
          }
          this.$emit('addDept')
          this.$emit('update:showDialog', false)
        }
      })
    },
    close() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.formData.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
